<?php

class Admin_UsersController extends Zend_Controller_Action
{

    public function init()
    {
        $option=array( 
        	"layout" => "layout", 
        	"layoutPath" => APPLICATION_PATH."/layouts/scripts/admin" 
      	); 
      	Zend_Layout::startMvc($option); 
      	
      	
      	$this->view->headTitle("User management - Admin Panel"); 
        	
        $baseUrl = $this->_request->getBaseurl();
        $this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $this->view->headLink()->appendStylesheet($baseUrl . "/public/css/admin.style.css");
        $this->view->headScript()->appendFile($baseUrl . "/public/js/jquery.js");
        
    }
    
    //Login khi chua Login thanh cong
	public function preDispatch() {
	    $auth = Zend_Auth::getInstance();
	    if (!$auth->hasIdentity()) {
	        if ($this->_request->getActionName() != 'login') {
	            $this->_redirect('/admin/login');
	        }
	    }
	}	
	

    public function indexAction()
    {
    	/*
    	$auth = Zend_Auth::getInstance ();
    	$infoUser = $auth->getIdentity();
    	var_dump($infoUser); */
    	
        $users = new Admin_Model_Users();
        $baseUrl = $this->_request->getBaseurl();
        
        $this->view->baseUrl = $baseUrl;
        $this->view->edit_link = $baseUrl . '/admin/users/edit/id/';
        $this->view->delete_link = $baseUrl . '/admin/users/delete/';
        $this->view->users = $users->getAll();
    }

    public function adduserAction(){
    	$frmAddUser = new Admin_Form_Adduserform();
    	$this->view->form = $frmAddUser;
    }
    
    public function addAction(){
    	//$this->_helper->layout()->disableLayout(); // neu nhu co su dung layout
    	//$this->_helper->viewRenderer->setNoRender(true);
    	
    	$username = $this->_request->getParam('username');
    	$password = $this->_request->getParam('password');
    	$realname = $this->_request->getParam('realname');
    		
    	$user = new Admin_Model_Users();
    	$user->username = $username;
    		
    	//Check Exist
    	if($this->checkExistUsernameAction($username)){
    		echo 'true';
    	}else{
    		$user->password = md5($password);
    		$user->realname = $realname;
    		$user->addUser($user);
    		echo 'add_ok';
    	}
    }
    
    
    public function editAction()
    {
    	$frmEditUser = new Admin_Form_Edituserform();
    	$id = $this->_request->getParam('id');
		
    	$user = new Admin_Model_Users();
    	$user->id = $id;
    	$userinfo = $user->getUserById();
    	$userinfo = $userinfo[0];
    	
    	$frmEditUser->getElement('id')->setValue($userinfo['id']);
    	$frmEditUser->getElement('username')->setValue($userinfo['username']);
    	$frmEditUser->getElement('realname')->setValue($userinfo['realname']);  
    	
    	$this->view->form = $frmEditUser;
    }
    
    
	public function updateAction(){
    	$id=$this->_request->getParam('id');
    	$username = $this->_request->getParam('username');
    	$password = $this->_request->getParam('password');
    	$realname = $this->_request->getParam('realname');
    	
    	$user=new Admin_Model_Users();
    	$user->id=$id;
    	$user->username = $username;
    	$user->password = $password;
    	$user->realname = $realname;
    	
    	$user->updateUser(); 
    	
    	$this->getHelper('viewRenderer')->setNoRender();
    	$this->_helper->redirector('index');
    }
    
    

    public function deleteAction(){
    	$id = $this->_request->getParam('id');
    	$user = new Admin_Model_Users();
    	$user->id = $id;
    	$user->deleteUser();
    	$this->getHelper('viewRenderer')->setNoRender();
    	$this->_helper->redirector('index');
    }
    
    
    public function checkExistUsernameAction($username) {
    	$this->_helper->layout()->disableLayout(); // neu nhu co su dung layout
    	$this->_helper->viewRenderer->setNoRender(true);
    	
    	//check ton tai
    	$users = new Admin_Model_Users();
    	$users->username = $username;
    	
    	return $users->getUserByUsername();
    }

}

